home *** CD-ROM | disk | FTP | other *** search
/ Winzipper / Winzipper_ISO.iso / programming / oracle7 7.2 / OCI72 / CDEMO5.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-27  |  6.7 KB  |  290 lines

  1. /*
  2.  *
  3.  *  An example program which demonstrates the use of
  4.  *  Cursor Variables in an OCI program.
  5.  *
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include <oratypes.h>
  13. #include <ocidfn.h>
  14. #include <ocikpr.h>
  15.  
  16.  /* oparse flags */
  17. #define DEFER_PARSE 1
  18. #define VERSION_7 2
  19.  
  20. #define NPOS 16
  21. #define DSCLEN 240
  22.  
  23. text *username = (text *) "SCOTT";
  24. text *password = (text *) "TIGER";
  25.  
  26. static  sword retval;
  27. static  ub1   hstb[256];
  28. static  text  errorb[4095];
  29. static  text  cbuf[NPOS][DSCLEN];
  30. static  sb4   cbufl[NPOS];
  31. static  sb4   dbsize[NPOS];
  32. static  sb4   dsize[NPOS];
  33. static  sb2   dbtype[NPOS];
  34. static  sb2   prec[NPOS];
  35. static  sb2   scale[NPOS];
  36. static  sb2   nullok[NPOS];
  37.  
  38. static Lda_Def lda1;
  39.  
  40. static text plsql_block[] = 
  41.       "begin \
  42.       OPEN :cursor1 FOR select empno, ename, job, mgr, hiredate,sal,deptno\
  43.                from emp where job=:job order by empno;\
  44.       end;";
  45.  
  46. /* CLIENT CURSORS */
  47. static Cda_Def cursor, cursor_emp;
  48.  
  49. /* Prototype */
  50. void oracle_error();
  51.  
  52. void main ()
  53. {
  54.   Lda_Def *ldap = &lda1;
  55.  
  56.   ub4    empno;
  57.   text   ename[11];
  58.   text   job[10];
  59.   ub4    mgr;
  60.   text   hidate[10];
  61.   ub4    sal;
  62.   ub4    deptno;
  63.   int    i;  
  64.   text   job_kind[50];
  65.   ub4    pos;
  66.   
  67.   strcpy((char *) job_kind, "ANALYST");
  68.   fprintf(stdout,"\n\nFETCHING for job=%s\n\n",job_kind);
  69.  
  70.   /* 
  71.    * Connect to Oracle as SCOTT/TIGER.
  72.    * Exit on any error.
  73.    */
  74.   
  75.   if (olog(ldap, hstb, username, -1, password, -1,
  76.            (text *) 0, -1, OCI_LM_DEF))
  77.   {  
  78.       printf("Unable to connect as %s\n", username);
  79.       exit(EXIT_FAILURE);
  80.   }
  81.   printf("Connected to Oracle as %s\n\n", username);
  82.  
  83.   /*
  84.    * Open a cursor for executing the PL/SQL block.
  85.    */
  86.  
  87.   if (oopen(&cursor, ldap, (text *) 0, -1, 0, (text *) 0, -1))
  88.   {
  89.      oracle_error(&cursor);
  90.      exit(EXIT_FAILURE);
  91.   }
  92.  
  93.   /*
  94.    * Parse the PL/SQL block.
  95.    */
  96.  
  97.   if (oparse(&cursor, plsql_block, (sb4) -1, (sword) TRUE, (ub4) 2))
  98.   {
  99.      oracle_error(&cursor);
  100.      exit(EXIT_FAILURE);
  101.   }
  102.  
  103.   /*
  104.    * Bind a variable of cursor datatype, the cursor will be opened
  105.    * inside the PL/SQL block.
  106.    */
  107.  
  108.   if (obndra(&cursor, (text *) ":cursor1", -1, (ub1 *) &cursor_emp, 
  109.              -1, SQLT_CUR, -1, (sb2 *) 0, (ub2 *) 0, (ub2 *) 0,
  110.              (ub4) 0, (ub4 *) 0, (text *) 0, 0, 0))
  111.   {
  112.      oracle_error(&cursor);
  113.      exit(EXIT_FAILURE);
  114.   }
  115.  
  116.   /*
  117.    * Bind a variable of string datatype.
  118.    */
  119.  
  120.   if (obndra(&cursor, (text *) ":job", -1, (ub1 *) job_kind, 
  121.              -1, SQLT_STR, -1, (sb2 *) 0, (ub2 *) 0, (ub2 *) 0,
  122.              (ub4) 0, (ub4 *) 0, (text *) 0, 0, 0))
  123.   {
  124.      oracle_error(&cursor);
  125.      exit(EXIT_FAILURE);
  126.   }
  127.  
  128.   /* 
  129.    * Execute the PL/SQL block.
  130.    */
  131.  
  132.    if (oexec(&cursor))
  133.    {
  134.       oracle_error(&cursor);
  135.       exit(EXIT_FAILURE);
  136.    }
  137.  
  138.   /*
  139.    * Close the cursor on which the PL/SQL block executed.
  140.    */
  141.  
  142.   if (oclose(&cursor))
  143.   {
  144.      oracle_error(&cursor);
  145.      exit(EXIT_FAILURE);
  146.   }
  147.  
  148.   /* 
  149.    * Do describe on cursor initialized and returned from the PL/SQL block.
  150.    */
  151.  
  152.   for (pos = 0; pos < NPOS;
  153. pos++)
  154.   {
  155.      cbufl[pos] = DSCLEN;
  156.      if (odescr(&cursor_emp, (sword) (pos+1), &dbsize[pos], &dbtype[pos],
  157.                 (sb1 *) cbuf[pos], &cbufl[pos], &dsize[pos],
  158.                 &prec[pos], &scale[pos], &nullok[pos]))
  159.      {
  160.         if (cursor_emp.rc == 1007)
  161.            break;
  162.         oracle_error(&cursor_emp);
  163.         exit(EXIT_FAILURE);
  164.      }
  165.   }
  166.  
  167.   printf("Describe select-list returns:\n\n");
  168.   printf("----------------------------------------\n");
  169.   printf("Item\t\tMaxSize\t\tType\n");
  170.   printf("----------------------------------------\n");
  171.   for (i = 0; i < (int)pos; i++)
  172.   {
  173.      cbuf[i][cbufl[i]] = '\0';
  174.      printf("%s\t\t%d\t\t%d\n", cbuf[i], dbsize[i], dbtype[i]);
  175.   }
  176.  
  177.   /* 
  178.    * Do client defines.
  179.    */
  180.  
  181.   if (odefin(&cursor_emp, 1, (ub1 *) &empno, (sword) sizeof(ub4), SQLT_INT, 
  182.              -1, (sb2 *) -1, (text *) 0, (sword) 0, (sword) 0, (ub2 *) 0,
  183.              (ub2 *) 0))
  184.   {
  185.      oracle_error(&cursor_emp);
  186.      exit(EXIT_FAILURE);
  187.   }
  188.  
  189.    if (odefin(&cursor_emp, 2, (ub1 *) ename, (sword) sizeof(ename), 
  190.               SQLT_STR, -1, (sb2 *) -1, (text *)0, (sword) 0, (sword) 0, 
  191.               (ub2 *) 0, (ub2 *) 0))
  192.    {
  193.       oracle_error(&cursor_emp);
  194.       exit(EXIT_FAILURE);
  195.    }
  196.  
  197.   if (odefin(&cursor_emp, 3, (ub1 *) job, (sword) sizeof(job), 
  198.              SQLT_STR, -1, (sb2 *) -1, (text *) 0, (sword) 0, (sword) 0, 
  199.              (ub2 *) 0, (ub2 *) 0))
  200.    {
  201.       oracle_error(&cursor_emp);
  202.       exit(EXIT_FAILURE);
  203.    }
  204.  
  205.   if (odefin(&cursor_emp, 4, (ub1 *)&mgr, (sword) sizeof(ub4), SQLT_INT, 
  206.              -1, (sb2 *) -1, (text *)0, (sword) 0, (sword) 0, (ub2 *) 0,
  207.              (ub2 *) 0))
  208.    {
  209.       oracle_error(&cursor_emp);
  210.       exit(EXIT_FAILURE);
  211.    }
  212.  
  213.   if (odefin(&cursor_emp, 5, (ub1 *)hidate, (sword) sizeof(hidate), 
  214.              SQLT_STR, -1, (sb2 *) -1, (text *) 0, (sword) 0, (sword) 0, 
  215.              (ub2 *) 0, (ub2 *) 0))
  216.    {
  217.       oracle_error(&cursor_emp);
  218.       exit(EXIT_FAILURE);
  219.    }
  220.  
  221.    if (odefin(&cursor_emp, 6, (ub1 *) &sal, (sword) sizeof(ub4), SQLT_INT, 
  222.           
  223.    -1, (sb2 *) -1, (text *) 0, (sword) 0, (sword) 0, (ub2 *) 0,
  224.               (ub2 *) 0))
  225.    {
  226.       oracle_error(&cursor_emp);
  227.       exit(EXIT_FAILURE);
  228.    }
  229.  
  230.    if (odefin(&cursor_emp, 7, (ub1 *) &deptno, (sword) sizeof(deptno), 
  231.               SQLT_INT, -1, (sb2 *) -1, (text *) 0, (sword) 0, (sword) 0, 
  232.               (ub2 *) 0, (ub2 *) 0))
  233.    {
  234.       oracle_error(&cursor_emp);
  235.       exit(EXIT_FAILURE);
  236.    }
  237.  
  238.   printf("\nFETCH from variable cursor:\n\n");
  239.   printf("------------------------------------------------------------\n");
  240.   printf("empno\tename\tjob\tmgr\thiredate\tsalary\tdept\n");
  241.   printf("------------------------------------------------------------\n");
  242.  
  243.   /* 
  244.    * Now fetch the result set and display.
  245.    */
  246.  
  247.   while (1)
  248.   {
  249.     sb4 err = 0;
  250.  
  251.     if (err = ofetch(&cursor_emp))
  252.     {
  253.       if (cursor_emp.rc == 1403)
  254.          break;
  255.       else 
  256.       {
  257.          oracle_error(&cursor_emp);
  258.          exit(EXIT_FAILURE);
  259.       }
  260.     }
  261.     else 
  262.     {
  263.       /* A row was returned; have to do the fetch.
  264. */
  265.       fprintf(stdout, "%d\t%s\t%s\t%d\t%s\t%d\t%d\n\n",
  266.               empno, ename, job, mgr, hidate, sal, deptno);
  267.     }
  268.   }
  269.  
  270.   /* 
  271.    * Log off.
  272.    */
  273.  
  274.   if (ologof(ldap))
  275.   {
  276.      oracle_error(&cursor_emp);
  277.      exit(EXIT_FAILURE);
  278.   }
  279. } /* end of main */
  280.  
  281. void oracle_error(lda)
  282. Lda_Def * lda;
  283. {
  284.   char msgbuf[512];
  285.   int n=oerhms(lda, lda->rc, msgbuf, (int) sizeof(msgbuf) );
  286.    
  287.   printf("\n\n%.*s\n",n,msgbuf);
  288. }
  289.  
  290.